<?php
/**
 * 展会展馆关联
 * Created by PhpStorm.
 * User: Apache
 * Date: 2019/10/27
 * Time: 15:41
 */

namespace app\api\controller\activemanage;
use app\api\controller\common\ApiBase;
use Think\Db;

class Exhibpavil extends apiBase
{
    protected $admin;
    protected $model = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->admin = session('admin');
        $this->model = model('Pavilion');
    }

    /**
     * 展会下的展馆列表
     * @return \think\response\Json|void
     */
    public function getList(){
        $exhib_id = $this->request->param('exhib_id');
        $list = $this->model->getListByExhib(['exhibition_id'=>$exhib_id]);
        return json(['code'=>1,'msg'=>'success','data'=>$list]);
    }

    /**
     * 增加展会下的展馆
     * @return \think\response\Json|void
     */
    public function add(){
        $exhib_id = $this->request->param('exhib_id');
        $pavilion_id = $this->request->param('pavilion_id');

        if(empty($exhib_id)||empty($pavilion_id)){
            return json(['code'=>0,'msg'=>'参数有误']);
        }

        #判断重复
        $info = db('exhibition_pavilion')->where(['exhibition_id'=>$exhib_id,'pavilion_id'=>$pavilion_id])->find();
        if($info){
            return json(['code'=>0,'msg'=>'该展馆已关联此展会，请勿重复添加']);
        }

        $r = $this->model->addByExhib(['exhibition_id'=>$exhib_id,'pavilion_id'=>$pavilion_id]);
        if(!$r){
            return json(['code'=>0,'msg'=>'操作失败，请重试']);
        }

        return json(['code'=>1,'msg'=>'操作成功']);
    }

    /**
     * 删除展会下的展馆
     * @return \think\response\Json|void
     */
    public function del(){
        $join_id = $this->request->param('join_id');
        $r = $this->model->delByExhib(['id'=>['in',$join_id]]);
        if(!$r){
            return json(['code'=>0,'msg'=>'操作失败，请重试']);
        }

        return json(['code'=>1,'msg'=>'操作成功']);
    }



    /**
     * 角摊费用列表
     *
     * @return void
     */
    public function getCornerList()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $exhibition_id = isset($post['exhibition_id']) && $post['exhibition_id'] != '' ? $post['exhibition_id'] : 0;
            if(!$exhibition_id){
                return json(array('code' => '0', 'msg' => '展会错误'));
            }
            $res=db('exhibition_corner')->where(['exhibition_id'=>$exhibition_id,'is_delete'=>0])->select();
            return json(array('code' => '1', 'msg' => '成功','data'=>$res));
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }


    /**
     * 角摊费用新增
     *
     * @return void
     */
    public function getCornerAdd()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $exhibition_id = isset($post['exhibition_id']) && $post['exhibition_id'] != '' ? $post['exhibition_id'] : 0;
            $price = isset($post['price']) && $post['price'] != '' ? $post['price'] : 0;
            $title = isset($post['title']) && $post['title'] != '' ? $post['title'] : '';
            $id = isset($post['id']) && $post['id'] != '' ? $post['id'] : 0;
            if(!$exhibition_id){
                return json(array('code' => '0', 'msg' => '展会错误'));
            }

            $findRes=db('exhibition_corner')->where(['id'=>$id])->find();
            $data['price']=$price;
            $data['title']=$title;
            $data['update_time']=time();
            $data['exhibition_id']=$exhibition_id;
            if (!$findRes) {
                $data['create_time']=time();
                $res=db('exhibition_corner')->insert($data);
            }else{
                $res=db('exhibition_corner')->where(['id'=>$id])->update($data);
            }
            if(!$res){
                return json(array('code' => '0', 'msg' => '操作失败'));
            }
            return json(array('code' => '1', 'msg' => '成功'));
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }

    /**
     * 角摊费用删除
     *
     * @return void
     */
    public function getCornerDel()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $id = isset($post['id']) && $post['id'] != '' ? $post['id'] : 0;
            $findRes=db('exhibition_corner')->where(['id'=>$id])->find();
            if (!$findRes) {
                return json(array('code' => '0', 'msg' => '信息错误'));
            }
            $res=db('exhibition_corner')->where(['id'=>$id])->update(['is_delete'=>1]);
            if(!$res){
                return json(array('code' => '0', 'msg' => '操作失败'));
            }
            return json(array('code' => '1', 'msg' => '成功'));
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }


}